package com.ruoyi.develop.goodsOrderInner.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.service.IGoodsOrderInService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.goodsOrderInner.domain.GoodsOrderInner;
import com.ruoyi.develop.goodsOrderInner.service.IGoodsOrderInnerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 厂内编码Controller
 * @author lmm
 * @date 2025-08-02
 */
@RestController
@RequestMapping("/goodsOrderInner/goodsOrderInner")
public class GoodsOrderInnerController extends BaseController {

    @Autowired
    private IGoodsOrderInnerService goodsOrderInnerService;
    @Autowired
    private IGoodsOrderInService goodsOrderInService;

    /**
     * 查询厂内编码列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsOrderInner:query')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsOrderInner entity) {
        startPage();
        List<GoodsOrderInner> list = goodsOrderInnerService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出厂内编码列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsOrderInner:export')")
    @Log(title = "厂内编码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsOrderInner entity) {
        List<GoodsOrderInner> list = goodsOrderInnerService.selectList(entity);
        ExcelUtil<GoodsOrderInner> util = new ExcelUtil<GoodsOrderInner>(GoodsOrderInner. class);
        util.exportExcel(response, list, "厂内编码数据");
    }

    /**
     * 获取厂内编码详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsOrderInner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsOrderInnerService.selectById(id));
    }

    /**
     * 新增厂内编码
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsOrderInner:add')")
    @Log(title = "厂内编码", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsOrderInner entity) {
        /* 根据存货编码 生成 内部编码 */
        GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(entity.getGoodsId());
        String goodsOrderInnerCode = goodsOrderInnerService.generateInnerCode(goodsOrderIn.getCode());
        entity.setCode(goodsOrderInnerCode);
        return toAjax(goodsOrderInnerService.insert(entity));
    }

    /**
     * 修改厂内编码
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsOrderInner:edit')")
    @Log(title = "厂内编码", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsOrderInner entity) {
        return toAjax(goodsOrderInnerService.update(entity));
    }

    /**
     * 删除厂内编码
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsOrderInner:remove')")
    @Log(title = "厂内编码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(goodsOrderInnerService.deleteByIds(ids));
    }
}
